﻿#include <QGuiApplication>
#include <QQmlApplicationEngine>
#include <QQmlEngine>
#include <QQmlComponent>
#include <QQmlContext>
#include <QObject>
#include <QThread>
#include <QFile>
#include <QJsonObject>
#include <QJsonArray>
#include <QDebug>
#include "windowsproxy.h"


int main(int argc, char *argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    
    QGuiApplication app(argc, argv);
    QQmlEngine  eng;
    //qmlRegisterType<MyClass>("MyClass.module",1,0,"MyClass");
    qDebug()<<"QSslSocket="<<QSslSocket::sslLibraryBuildVersionString();
    qDebug() << "OpenSSL支持情况:" << QSslSocket::supportsSsl();
    
    if(argc >1)
    {
        //代表命令行启动
        auto str = argv[1];
        QByteArray qtStr(str);
        if(qtStr == "nogui")
        {
            //服务器ip
            QByteArray Server_ip(argv[2]);
            //要转发的内网ip
            QByteArray AppHost(argv[3]);
            //要转发的内网ip其端口
            QString AppPort = QByteArray(argv[4]);
            //部署在公网的端口
            QString ForwardPort = QByteArray(argv[5]);
            qDebug()<<Server_ip<<" "<<AppHost<<" "<<AppPort<<" "<<ForwardPort;
            WindowsProxy* proxy = new WindowsProxy();
            QJsonObject obj;
            obj["server_ip"] = (QString)Server_ip;
            obj["appHost"] = (QString)AppHost;
            obj["appPort"] = AppPort;
            obj["forwardPort"] = ForwardPort;
            qDebug()<<QJsonDocument(obj).toJson();
            proxy->connectToServer(Server_ip);
            proxy->openForward(QJsonDocument(obj).toJson());
        }
    }else{
        WindowsProxy* proxy = new WindowsProxy();
        eng.rootContext()->setContextProperty("proxy",proxy);  //注册成为QML的组件
        
        QObject *obj = nullptr;
        QQmlComponent compent(&eng,QUrl(QStringLiteral("qrc:/main.qml")));
        obj = compent.create();
    }
    return app.exec();
}
